import { vitePluginHtml } from "./script/vite-plugin-html"
import { getViteProxy, getViteEnv as getViteEnvironment } from "./script/utils"
import { visualizer } from "rollup-plugin-visualizer"
import { createVuePlugin } from "vite-plugin-vue2"
import importCdns from "./script/build-import-cdn"
import importToCDN from "vite-plugin-cdn-import"
import commonjs from "rollup-plugin-commonjs"
import eslint from "vite-plugin-eslint"
import { defineConfig } from "vite"
import path from "node:path"

export default defineConfig(({ command, mode }) => {
  const { VITE_PUBLIC_DIR, VITE_PROXY, VITE_BASE } = getViteEnvironment(mode)
  return {
    plugins: [
      eslint({
        exclude:["./src/components/o-*/**"]
      }),
      createVuePlugin(), 
      vitePluginHtml(),
      importToCDN(importCdns),
      visualizer(),
      commonjs(),
    ],
    publicDir: VITE_PUBLIC_DIR,
    extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
        views: path.resolve(__dirname, "src/views/"),
      },
    },
    esbuild: {
      pure: ["console.log"],
      minify: true,
    },
    css: {
      loaderOptions: {
        postcss: {
          plugins: [require("tailwindcss"), require("autoprefixer")]
        }
      },
      // postcss: {
      //   plugins: [
      //     {
      //       postcssPlugin: "internal:charset-removal",
      //       AtRule: {
      //         charset: (atRule) => {
      //           if (atRule.name === "charset") {
      //             atRule.remove()
      //           }
      //         },
      //       },
      //     },
      //   ],
      // },
    },
    server: {
      proxy: getViteProxy(VITE_PROXY),
    },
    base:VITE_BASE,
    build: {
      sourcemap:true,
      // 指定生成静态资源的存放路径
      assetsDir: "static",
      rollupOptions:{
        output:{
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id.toString().split("node_modules")[1].split("/")[0].toString()
            }
          },
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        },
        lintOnSave: false,
      }
    },
  }
})
